package rpg.core
{
	import rpg.Utils.GameUtils;
	import rpg.interfaces.IUpdate;

	/**
	 * 有更新队列的组件
	 * @author hyy
	 *
	 */
	public class FrameComponent extends GameComponent implements IUpdate
	{
		/**
		 * 更新帧数
		 */
		protected var _frameRate : uint = 0;
		/**
		 * 更新优先级
		 */
		protected var _priority : int;
		/**
		 * 更新间隔
		 */
		protected var _frameInterval : uint = 0;
		/**
		 * 延迟
		 */
		protected var frameElapsedTime : uint = 0;
		/**
		 * 是否需要检测更新
		 */
		protected var checkUpdateable : Boolean = false;
		/**
		 * 是否销毁
		 */
		protected var _isDestroy : Boolean = false;

		public function FrameComponent(type : *)
		{
			super(type);
		}

		public function update(delay : uint) : void
		{

		}

		public function checkUpdatable(elapsedTime : uint) : Boolean
		{
			if(!checkUpdateable)
			{
				return true;
			}
			frameElapsedTime += elapsedTime;
			if(frameElapsedTime >= _frameInterval)
			{
				frameElapsedTime -= _frameInterval;
				return true;
			}
			return false;
		}

		public function get frameRate() : uint
		{
			return _frameRate;
		}

		public function set frameRate(value : uint) : void
		{
			if(value <= 0 || value >= GameUtils.frameRate)
			{
				_frameRate = GameUtils.frameRate;
				checkUpdateable = false;
				return;
			}
			_frameRate = value;
			_frameInterval = 1000 / _frameRate;
			checkUpdateable = true;
		}

		public function get priority() : int
		{
			return _priority;
		}

		public function set priority(value : int) : void
		{
			_priority = value;
		}

		public function registerd(level : int = 0 , priority : int = 0) : void
		{
		}

		public function unRegisterd() : void
		{
		}

		/**
		 * 更新间隔
		 */
		public function get frameInterval() : uint
		{
			return _frameInterval;
		}

		/**
		 * @private
		 */
		public function set frameInterval(value : uint) : void
		{
			_frameInterval = value;
			checkUpdateable = true;
		}

		/**
		 * 是否销毁
		 */
		public function get isDestroy() : Boolean
		{
			return _isDestroy;
		}

		/**
		 * @private
		 */
		public function set isDestroy(value : Boolean) : void
		{
			_isDestroy = value;
		}


	}
}